#e
#Title[Xu-78x̐Î~v]
#Text[]
#ScriptVersion[2]

script_enemy_main {
    let imgBoss = GetCurrentScriptDirectory~".\chirno.png";

    @Initialize {

        LoadGraphic(imgBoss);
        SetTexture(imgBoss);
        SetGraphicRect(0, 0, 63, 63);
        SetLife(5000); 
        Tshot;
        Tshot2;
        Tshot3;
        Tmove;
        CutIn(YOUMU,"Xu-78x̐Î~v", "", 0, 0, 0, 0);
        SetTimer(60);
        SetScore(4000000);
        SetInvincibility(600);

    }

    @MainLoop {
        SetCollisionA(GetX, GetY, 24);
        SetCollisionB(GetX, GetY, 24);

        yield;
    }

    @DrawLoop {
        DrawGraphic(GetX, GetY);
    }

    @Finalize {
        DeleteGraphic(imgBoss);
    }

    sub standBy {

        SetMovePosition02(228, 136, 30);
        setGraphicMove;
        loop(30){yield;}
        setGraphicStop;
        loop(70){yield;}
    }

    sub setGraphicStop  { SetGraphicRect(  0,   0, 128, 128); }
    sub setGraphicLeft  { SetGraphicRect(256,   0, 384, 128); }
    sub setGraphicRight { SetGraphicRect(256, 256, 384, 384); }

    sub setGraphicMove {
        if(GetSpeedX < 0) {
            setGraphicLeft;
        } else {
            setGraphicRight;
        }
    }


    task Tshot{
        yield;
       loop(100){yield;}
         let id = 1;
       loop{            
            CreateShotA(id,rand(0,448),0,0);
            SetShotDataA(id,0,1,rand(80,100),0,0,1,AQUA03);
            SetShotDataA(id,rand_int(2,16)*50,0,rand(80,100),0,0,0,BLUE03);
            FireShot(id);
           loop(16){yield;}
           id ++;
         }     
      }
    task Tshot2{
        yield;
       loop(100){yield;}
         let id = 100000000;
       loop{            
            CreateShotA(id,rand(0,448),0,0);
            SetShotDataA(id,0,1,rand(80,100),0,0,1,AQUA02);
            SetShotDataA(id,rand_int(3,30)*30,0,rand(80,100),0,0,0,BLUE02);
            FireShot(id);
           loop(9){yield;}
           id ++;
         }     
      }
    task Tshot3{
        yield;
       loop(100){yield;}
         let id = 200000000;
       loop{            
            CreateShotA(id,rand(0,448),0,0);
            SetShotDataA(id,0,1,rand(80,100),0,0,1,AQUA01);
            SetShotDataA(id,rand(100,700),0,rand(80,100),0,0,0,BLUE01);
            FireShot(id);
           loop(9){yield;}
           id ++;
         }     
      }


    function nway(dir, way, span, color) {

        let radius = 32;


        let angle  = dir - (way - 1) / 2 * span;

        loop(way) {

            CreateShot01(GetX, GetY, 1, angle, color, 0);
            angle += span;
        }
    }
    task Tmove {
        yield;
        standBy;
        loop {
            loop(80) { yield; }
            moveToPlayer(rand(20, 40), rand(-20, 20), 40,
                         GetClipMinX + 32, GetClipMinY + 32,
                         GetClipMaxX - 32, GetClipMinY + 160);
            setGraphicMove;
            loop(40){yield;}
            setGraphicStop;
        }
    }



    function moveToPlayer(xMove, yAdd, frame, left, top, right, bottom) {
        let x;
        let y;

        if(GetPlayerX < GetX) {

            x = GetX - xMove;

            if(x < left) {
                x = GetX + xMove;
            }
        } else {

            x = GetX + xMove;


            if(right < x) {
                x = GetX - xMove;
            }
        }

        y = GetY + yAdd;
        if(y < top) {
            y = top;
        } else if(bottom < y) {
            y = bottom;
        }

        SetMovePosition02(x, y, frame);
    }
}